<?php

namespace app\admin\controller;

use think\Controller;
use app\common\model\Contents as M;
use app\common\model\Categorys;
use think\Model;

class Contents extends Controller {
    /*
     * 列表页
     * 
     */

    public function index() {
        $Cate = new Categorys();
        $rs = $Cate->getAll();
        $this->assign('rs', $rs); //分类
        return $this->fetch();
    }

    /*
     * 内容列表页获取josn数据
     * 
     */
    public function get_josn() {
        $m = new M();
        $Cate = new Categorys();
        $rs = $m->getListAll(1);
        $all = $m->getListAll(2);
        $count = count($all);
        $lists = ['code' => 0, 'msg' => '', 'count' => $count, 'data' => []];
        foreach ($rs as $k => $v) {
            $v['hits'] = $v['hits'] == 1 ? "是" : "否";
            $v['goods'] = $v['goods'] == 1 ? "是" : "否";
            $v['status'] = public_status($v['status']);
            $v['cid'] = $Cate->getById($v['cid'])->name;
            $lists['data'][] = $v;
        }
        return $lists;
    }

    /*
     * 内容添加页面
     * 
     */
    public function add() {
        $Cate = new Categorys();
        $rs = $Cate->getAll();
        $this->assign('rs', $rs); //分类
        return $this->fetch();
    }

    /*
     * 获取修改页面数据
     * 
     */

    public function edit() {
        $m = new M(); //实例化Cntents类
        $Cate = new Categorys(); //实例化分类
        $rs = $Cate->getAll(); //获取列表页
        $s = $m->getById(input('id')); //获取分类详情数据
        $gallery = unserialize($s->gallery); //获取图集数据
        if (empty($gallery)) {
            $gallery = [];
        }
        $this->assign('gallery', $gallery); //向页面传送图集
        $this->assign('rs', $rs); //向页面传送分类详情
        $this->assign('s', $s); //向页面传送列表信息
        return $this->fetch();
    }

    /*
     * 内容提交数据
     * 
     */

    public function contents_submit() {
        $m = new M();
        if (input('id', 0) == 0) {//添加
            $opt = [
                'cid' => input('cid'),
                'title' => input('title'),
                'subtitle' => input('subtitle'),
                'keywords' => input('keywords'),
                'description' => input('description'),
                'pic' => input('pic'),
                'sort' => input('sort'),
                'content' => input('content'),
                'author' => input('author'),
                'status' => input('status'),
                'date' => input('date'),
                'time' => input('date'),
            ];

            $opt['hits'] = input('hits', 0);
            $opt['goods'] = input('goods', 0);
            $gallery = input('gallery/a');
            if (!empty($gallery)) {
                $opt['gallery'] = serialize($gallery);
            }
            $rs = $m->add($opt);
            return js_return($rs[0], $rs[1]);
        } else {//修改
            $opt = [
                'id' => input('id'),
                'cid' => input('cid'),
                'title' => input('title'),
                'subtitle' => input('subtitle'),
                'keywords' => input('keywords'),
                'description' => input('description'),
                'pic' => input('pic'),
                'sort' => input('sort'),
                'content' => input('content'),
                'author' => input('author'),
                'status' => input('status'),
                'date' => input('date'),
                'time' => input('date'),
            ];
            $opt['hits'] = input('hits', 0);
            $opt['goods'] = input('goods', 0);
            $gallery = input('gallery/a');

            if (!empty($gallery)) {
                $opt['gallery'] = serialize($gallery);
            }
            $rs = $m->modify($opt, input('id'));
            return js_return($rs[0], $rs[1]);
        }
    }

    /*
     * 删除
     * 
     */

    public function del_submit() {
        $m = new M();
        $result = $m->destroy(input('id'));
        $m->node_update(input('id'));
        if (false !== $result) {
            return js_return(1, "删除成功");
        } else {
            return js_return(0, $this->getError());
        }
    }

}
